import json
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QLineEdit, QComboBox, QVBoxLayout, QWidget, \
    QMessageBox, QHBoxLayout
from PyQt5.QtGui import QPixmap, QFont, QIcon, QPalette, QBrush
from PyQt5.QtCore import Qt, QFile, QTextStream

from content.qianwen import generate_outline
from main3 import TextGameUI

'''
只是一个简陋的主界面，需要重写或加样式
'''

class GameMainUI(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        # 窗口设置
        self.setWindowTitle("文字互动游戏 - 主菜单")
        self.setGeometry(50, 50, 1100, 600)

        pixmap = QPixmap("img/org_bg.png")
        palette = QPalette()
        palette.setBrush(QPalette.Window, QBrush(pixmap))
        self.setPalette(palette)

        # 创建中心部件和布局
        central_widget = QWidget()
        layout = QVBoxLayout()
        central_widget.setLayout(layout)
        self.setCentralWidget(central_widget)

        # 标题
        title_label = QLabel("欢迎来到文字冒险游戏", self)
        title_label.setFont(QFont('Arial', 24))
        title_label.setAlignment(Qt.AlignCenter)
        title_label.setStyleSheet("color: white;")  # 设置标题为白色
        layout.addWidget(title_label)

        # 输入框和下拉菜单布局
        input_layout = QHBoxLayout()
        layout.addLayout(input_layout)

        # 用户名输入框
        self.name_input = QLineEdit(self)
        self.name_input.setPlaceholderText("请输入你的名字")
        self.name_input.setFixedSize(350, 70)
        # layout.addWidget(self.name_input)
        input_layout.addWidget(self.name_input)

        # 故事背景输入框
        self.background_input = QLineEdit(self)
        self.background_input.setPlaceholderText("请简要描述你想要的故事背景")
        self.background_input.setFixedSize(350, 70)
        # layout.addWidget(self.background_input)
        input_layout.addWidget(self.background_input)

        # 下拉菜单布局
        combo_layout = QHBoxLayout()
        layout.addLayout(combo_layout)

        # 故事风格选择下拉菜单
        self.style_combo = QComboBox(self)
        self.style_combo.setFixedSize(350, 70)
        self.style_combo.addItems([
            "奇幻", "科幻", "恐怖", "历史",
            "浪漫", "悬疑", "冒险", "神话",
            "现实主义", "童话", "惊悚", "西部",
            "战争", "传记", "生态", "魔幻现实主义"
        ])
        combo_layout.addWidget(self.style_combo)

        # layout.addWidget(self.style_combo)

        # 语言风格选择下拉菜单
        self.language_combo = QComboBox(self)
        self.language_combo.setFixedSize(350, 70)
        self.language_combo.addItems([
            "正式", "幽默", "严肃",
            "亲切", "讽刺", "抒情",
            "活泼", "学术", "对话式",
            "神秘", "乐观", "悲观"
        ])
        # layout.addWidget(self.language_combo)
        combo_layout.addWidget(self.language_combo)


        # 开始按钮
        start_button = QPushButton("开始游戏", self)
        start_button.clicked.connect(self.start_game)
        start_button.setFixedSize(300, 60)
        title_label.setFont(QFont('Arial', 20))
        # title_label.setAlignment(Qt.AlignCenter)
        # 创建水平布局来放置开始按钮
        button_layout = QHBoxLayout()
        # 将开始按钮添加到水平布局中


        # 设置水平布局的左右边距，使其居中
        button_layout.addStretch(1)  # 在按钮之前添加伸缩空间
        button_layout.addWidget(start_button)

        button_layout.addStretch(1)  # 在按钮之后添加伸缩空间

        # 将水平布局添加到主布局中
        layout.addLayout(button_layout)

        self.loadStyleSheet()

    def loadStyleSheet(self):
        # 读取QSS文件
        file = QFile("./style_init.qss")
        if file.open(QFile.ReadOnly | QFile.Text):
            stream = QTextStream(file)
            style = stream.readAll()
            self.setStyleSheet(style)  # 应用样式
        file.close()


    def start_game(self):
        name = self.name_input.text().strip()
        background = self.background_input.text().strip()
        story_style = self.style_combo.currentText()
        language_style = self.language_combo.currentText()

        if not name or not background:
            QMessageBox.warning(self, "警告", "请填写完整的信息！")
            return

        # 假设这是与AI API交互的部分
        game_title, game_outline = self.generate_game_outline(name, background, story_style, language_style)

        if game_title and game_outline:
            self.open_game_ui(game_title, game_outline, language_style)

    def generate_game_outline(self, name, background, story_style, language_style):
        # 这里调用AI生成游戏大纲的逻辑
        response = generate_outline(name, background, story_style, language_style)
        response = json.loads(response)
        title = response.get('game_title')
        outline = response.get('game_synopsis')
        print(f"游戏标题：{title}")
        print(f"游戏梗概：{outline}")
        return title, outline

    def open_game_ui(self, title, outline, language_style):
        self.game_window = TextGameUI(title, outline, language_style)  # 传递游戏大纲给TextGameUI
        self.game_window.show()
        self.hide()  # 隐藏主菜单窗口

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = GameMainUI()
    window.show()
    sys.exit(app.exec_())